package cn.jly.flink.source2sink.redis;

import cn.jly.flink.entity.Person;
import cn.jly.flink.utils.ConstantUtils;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.RandomUtils;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

import java.util.HashSet;
import java.util.UUID;

/**
 * @PackageName cn.jly.flink.source2sink.redis
 * @ClassName RedisTest
 * @Description
 * @Author 姬岚洋
 * @Date 2021/1/15 下午 3:49
 */
public class RedisTest {
    public static void main(String[] args) {
        String ip = "192.168.172.101";
        final HashSet<HostAndPort> set = new HashSet<>(6);
        set.add(new HostAndPort(ip, 7001));
        set.add(new HostAndPort(ip, 7002));
        set.add(new HostAndPort(ip, 7003));
        set.add(new HostAndPort(ip, 7004));
        set.add(new HostAndPort(ip, 7005));
        set.add(new HostAndPort(ip, 7006));

        try(final JedisCluster jedisCluster = new JedisCluster(set)){
            for (int i = 0; i < 5; i++) {
                final String lastName = UUID.randomUUID().toString().substring(0, 5);
                final Person person = Person.builder()
                        .id(ConstantUtils.ID_GENERATOR.getAndIncrement())
                        .lastName(lastName)
                        .email(lastName + "@qq.com")
                        .age(RandomUtils.nextInt(1, 100))
                        .gender(RandomUtils.nextInt(0, 2))
                        .build();

                jedisCluster.hset(person.getClass().getSimpleName(), String.valueOf(person.getId()), JSON.toJSONString(person));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
